package chen.web.forum;

import java.util.List;

import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;

import chen.web.DataSourceProvider;
import chen.web.event.EventListener;
import chen.web.forum.ReplyCondition.Order;
import chen.web.forum.event.PostCheckedEvent;
import chen.web.forum.event.PostDeletedEvent;
import chen.web.forum.event.PostPublishedEvent;
import chen.web.forum.event.PostRestoredEvent;
import chen.web.forum.event.PostType;
import chen.web.util.DateInt;

public class TopicInfoListeners {
	
	private static SimpleJdbcTemplate simpleJdbcTemplate = new SimpleJdbcTemplate(DataSourceProvider.getDataSource());
	private static final String SQL_1 = "update web_subject set sub_replyCount = sub_replyCount-1, sub_lastReplierId = ?, sub_lastReplierNickname = ?, sub_lastReplyDate = ? where sub_id = ?;";
	private static final String SQL_2 = "update web_subject set sub_replyCount = sub_replyCount+1, sub_lastReplierId = ?, sub_lastReplierNickname = ?, sub_lastReplyDate = ? where sub_id = ?;";

	public static class PostDeletedListener implements EventListener<PostDeletedEvent>{
		private TopicManager tm;
		public PostDeletedListener(TopicManager tm){
			this.tm = tm;
		}
		@Override
		public void listen(PostDeletedEvent event) {
			// TODO Auto-generated method stub
			if(event.getPostType() == PostType.Reply){
				Reply reply = (Reply)event.getEntity();
				//已经假删除过了，此次真删除不操作
				if(reply.isFakeDelete() && !event.isFake()){
					return;
				}
				List<Reply> replyList = tm.getReplyList(new ReplyCondition().setSubject(reply.getBelongSubject()).setOrder(Order.Desc), 0, 1);
				//没有回复了
				if(replyList.size() == 0){
					Subject belong = reply.getBelongSubject();
					simpleJdbcTemplate.update(SQL_1, belong.getAuthor().getId(), belong.getAuthor().getAccount().getNickname(), DateInt.transform(belong.getDateline()), belong.getId());
				} else {
					Reply lastReply = replyList.get(0);
					simpleJdbcTemplate.update(SQL_1, lastReply.getAuthor().getId(), lastReply.getAuthor().getAccount().getNickname(), DateInt.transform(lastReply.getDate()), lastReply.getBelongSubject().getId());						
				}
			}
		}
	}
	
	public static class PostPublishedListener implements EventListener<PostPublishedEvent>{
		@Override
		public void listen(PostPublishedEvent event) {
			// TODO Auto-generated method stub
			if(event.getPostType() == PostType.Reply){
				Reply reply = (Reply)event.getEntity();
				if(!reply.isNeedChecking()){
					ForumAccount author = reply.getAuthor();
					simpleJdbcTemplate.update(SQL_2, author.getId(), author.getAccount().getNickname(), DateInt.transform(reply.getDate()), reply.getBelongSubject().getId());	
				}
			}
		}
	}
	
	public static class PostCheckedListener implements EventListener<PostCheckedEvent>{
		@Override
		public void listen(PostCheckedEvent event) {
			// TODO Auto-generated method stub
			if(event.getPostType() == PostType.Reply){
				Reply reply = (Reply)event.getEntity();
				ForumAccount author = reply.getAuthor();
				simpleJdbcTemplate.update(SQL_2, author.getId(), author.getAccount().getNickname(), DateInt.transform(reply.getDate()), reply.getBelongSubject().getId());	
			}
		}
	}
	
	public static class PostRestoredListener implements EventListener<PostRestoredEvent>{

		@Override
		public void listen(PostRestoredEvent event) {
			// TODO Auto-generated method stub
			if(event.getPostType() == PostType.Reply){
				Reply reply = (Reply)event.getEntity();
				ForumAccount author = reply.getAuthor();
				simpleJdbcTemplate.update(SQL_2, author.getId(), author.getAccount().getNickname(), DateInt.transform(reply.getDate()), reply.getBelongSubject().getId());	
			}
		}
	
	}
}
